package com.totoro.view.tags;

import com.totoro.utils.NumberToCN;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author TOTORO
 * @date 2021/6/7 14:33
 */
@Getter
@Setter
public class NumberToCnTags extends SimpleTagSupport {
    private String value;

    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = this.getJspContext().getOut();
        // 1.获取标签体内容
        StringWriter sw = new StringWriter();
        this.getJspBody().invoke(sw);
        sw.close();
        // 2.处理标签体内容
        try {
            double money = Double.parseDouble(sw.toString());
            BigDecimal bigDecimal = new BigDecimal(money);
            String rs = NumberToCN.number2CNMontrayUnit(bigDecimal);
            out.print("<span id='number-cn'>"+rs+"</span>");
        } catch (Exception e){
            out.print(" ");
        }

        // 3.重新输入标签体内容
    }
}
